package com.andatsoft.laisim.theme;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Environment;

import com.andatsoft.laisim.db.DataBaseAccess;
import com.andatsoft.laisim.entity.SkinItem;
import com.andatsoft.laisim.entity.ThemeItem;

public class ThemeManager {

	public static final int MAIN_ACTIVYTY = 0;
	public static final int SOUND_EFFECT_ACTIVYTY = 1;
	public static final int LIBRARY_ACTIVYTY = 2;
	public static final int PLAYER_SERVICE = 3;
	public static final int TOOLBOX_PLAYER = 4;

	private static Theme mSelectedTheme;
	private static Skin mSelectedSkin;
	private static String[] mBuiltInThemeList = { "Default", "Blue", "Pink",
			"Red", "White and transparent" };
	private static String[] mBuiltInSkinList = { "Default" };

	public static void saveTheme(Theme mTheme) {
		try {
			File f = new File("D:/");
			f.mkdir();
			FileOutputStream fos = new FileOutputStream(f + "/pink.lst");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(mTheme);
			oos.flush();
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static boolean loadTheme(Context c) {
		DataBaseAccess db = DataBaseAccess.getInstance(c
				.getApplicationContext());
		ThemeItem tI = db.getSelectedTheme();
		if (tI == null) {
			importBuiltInTheme(db);
			tI = db.getSelectedTheme();
		}
		int id = tI.id;
		if (id <= 5) {
			String url = "default.lst";
			switch (id) {
			case 1: // default
				url = "default.lst";
				break;
			case 2: // blue
				url = "blue.lst";
				break;
			case 3: // pink
				url = "pink.lst";
				break;
			case 4: // red
				url = "red.lst";
				break;
			case 5: // wat
				url = "whiteatrans.lst";
				break;

			}
			ThemeManager.loadLaisimTheme(c, url, true);
		} else {
			ThemeManager.loadLaisimTheme(c, tI.address, false);
		}

		return true;

	}

	public static Theme getSelectedTheme() {
		return mSelectedTheme;
	}

	public static Skin getSelectedSkin() {
		return mSelectedSkin;
	}

	public static void importBuiltInTheme(DataBaseAccess db) {
		db.dropThemeTable();
		for (int i = 0; i < mBuiltInThemeList.length; i++) {
			if (i == 0) {
				db.addThemeInfo(mBuiltInThemeList[i], "Default", "", true);
			} else {
				db.addThemeInfo(mBuiltInThemeList[i], "Default", "", false);
			}
		}
	}

	public static void importBuiltInSkin(DataBaseAccess db) {
		db.dropSkinTable();
		for (int i = 0; i < mBuiltInSkinList.length; i++) {
			if (i == 0) {
				db.addSkinInfo(mBuiltInSkinList[i], "Default", "", true);
			} else {
				db.addSkinInfo(mBuiltInSkinList[i], "Default", "", false);
			}
		}
	}

	// public static boolean saveLaisimTheme(String output, Theme theme) {
	// DocumentBuilderFactory docFactory = DocumentBuilderFactory
	// .newInstance();
	// try {
	// DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
	//
	// // create the root element
	// Document doc = docBuilder.newDocument();
	// Element rootEle = doc.createElement("LaisimTheme");
	// doc.appendChild(rootEle);
	//
	// // create theme elements (27 node)
	// // create child node
	// // Header screen title color
	// Element node = doc.createElement("HSTitleColor");
	// node.appendChild(doc.createTextNode(theme.getHSTitleColor() + ""));
	// rootEle.appendChild(node);
	//
	// // Header screen artist color
	// node = doc.createElement("HSArtistColor");
	// node.appendChild(doc.createTextNode(theme.getHSArtistColor() + ""));
	// rootEle.appendChild(node);
	//
	// // Header screen background alpha
	// node = doc.createElement("HSBackgroundAlpha");
	// node.appendChild(doc.createTextNode(theme.getHSBackgroundAlpha()
	// + ""));
	// rootEle.appendChild(node);
	//
	// // Footer screen background alpha
	// node = doc.createElement("FSBackgroundAlpha");
	// node.appendChild(doc.createTextNode(theme.getFSBackgroundAlpha()
	// + ""));
	// rootEle.appendChild(node);
	//
	// // Main pager album art background alpha
	// node = doc.createElement("MPAAlhpha");
	// node.appendChild(doc.createTextNode(theme.getMPAAlhpha() + ""));
	// rootEle.appendChild(node);
	//
	// // Main pager lyrics text color
	// node = doc.createElement("MPLSColor");
	// node.appendChild(doc.createTextNode(theme.getMPLSColor() + ""));
	// rootEle.appendChild(node);
	//
	// // Main pager lyrics text size
	// node = doc.createElement("MPLSSize");
	// node.appendChild(doc.createTextNode(theme.getMPLSSize() + ""));
	// rootEle.appendChild(node);
	//
	// // Main pager lyrics background alpha
	// node = doc.createElement("MPLSAlpha");
	// node.appendChild(doc.createTextNode(theme.getMPLSAlpha() + ""));
	// rootEle.appendChild(node);
	//
	// // Main pager list background alpha
	// node = doc.createElement("MPLBackgroundAlpha");
	// node.appendChild(doc.createTextNode(theme.getMPLBackgroundAlpha()
	// + ""));
	// rootEle.appendChild(node);
	//
	// // Main pager list normal artist color
	// node = doc.createElement("MPLNormalArtistColor");
	// node.appendChild(doc.createTextNode(theme.getMPLNormalArtistColor()
	// + ""));
	// rootEle.appendChild(node);
	//
	// // Main pager list normal artist size
	// node = doc.createElement("MPLNormalArtistSize");
	// node.appendChild(doc.createTextNode(theme.getMPLNormalArtistSize()
	// + ""));
	// rootEle.appendChild(node);
	//
	// // Main pager list normal title color
	// node = doc.createElement("MPLNormalTitleColor");
	// node.appendChild(doc.createTextNode(theme.getMPLNormalTitleColor()
	// + ""));
	// rootEle.appendChild(node);
	//
	// // Main pager list normal title size
	// node = doc.createElement("MPLNormalTitleSize");
	// node.appendChild(doc.createTextNode(theme.getMPLNormalTitleSize()
	// + ""));
	// rootEle.appendChild(node);
	//
	// // Main pager list selected artist color
	// node = doc.createElement("MPLSelectedArtistColor");
	// node.appendChild(doc.createTextNode(theme
	// .getMPLSelectedArtistColor() + ""));
	// rootEle.appendChild(node);
	//
	// // Main pager list selected artist size
	// node = doc.createElement("MPLSelectedArtistSize");
	// node.appendChild(doc.createTextNode(theme
	// .getMPLSelectedArtistSize() + ""));
	// rootEle.appendChild(node);
	//
	// // Main pager list selected title color
	// node = doc.createElement("MPLSelectedTitleColor");
	// node.appendChild(doc.createTextNode(theme
	// .getMPLSelectedTitleColor() + ""));
	// rootEle.appendChild(node);
	//
	// // Main pager list selected title size
	// node = doc.createElement("MPLSelectedTitleSize");
	// node.appendChild(doc.createTextNode(theme.getMPLSelectedTitleSize()
	// + ""));
	// rootEle.appendChild(node);
	//
	// // Normal general text size
	// node = doc.createElement("NormalTextColor");
	// node.appendChild(doc.createTextNode(theme.getNormalTextColor() + ""));
	// rootEle.appendChild(node);
	//
	// // Selected general text size
	// node = doc.createElement("SelectedTextColor");
	// node.appendChild(doc.createTextNode(theme.getSelectedTextColor()
	// + ""));
	// rootEle.appendChild(node);
	//
	// // Normal item list alpha
	// node = doc.createElement("NormalItemListAlpha");
	// node.appendChild(doc.createTextNode(theme.getNormalItemListAlpha()
	// + ""));
	// rootEle.appendChild(node);
	//
	// // Selected item list alpha
	// node = doc.createElement("SelectedItemListAlpha");
	// node.appendChild(doc.createTextNode(theme
	// .getSelectedItemListAlpha() + ""));
	// rootEle.appendChild(node);
	//
	// // Selected general text size
	// node = doc.createElement("DialogBackgroundColor");
	// node.appendChild(doc.createTextNode(theme
	// .getDialogBackgroundColor() + ""));
	// rootEle.appendChild(node);
	//
	// // Normal item list alpha
	// node = doc.createElement("DialogTextColor");
	// node.appendChild(doc.createTextNode(theme.getDialogTextColor() + ""));
	// rootEle.appendChild(node);
	//
	// // Dialog text selected color
	// node = doc.createElement("DialogTextSelectedColor");
	// node.appendChild(doc.createTextNode(theme
	// .getDialogTextSelectedColor() + ""));
	// rootEle.appendChild(node);
	//
	// // Transition alpha
	// node = doc.createElement("TransitionAlpha");
	// node.appendChild(doc.createTextNode(theme.getTransitionAlpha() + ""));
	// rootEle.appendChild(node);
	//
	// // Grow color effect
	// node = doc.createElement("GrowColorEffect");
	// node.appendChild(doc.createTextNode(theme.getGrowColorEffect() + ""));
	// rootEle.appendChild(node);
	//
	// // Edge color effect
	// node = doc.createElement("EdgeColorEffect");
	// node.appendChild(doc.createTextNode(theme.getEdgeColorEffect() + ""));
	// rootEle.appendChild(node);
	//
	// // write to xml file
	// TransformerFactory transFactory = TransformerFactory.newInstance();
	// Transformer trans = transFactory.newTransformer();
	//
	// DOMSource domSource = new DOMSource(doc);
	// StreamResult result = new StreamResult(output);
	//
	// trans.setOutputProperty(OutputKeys.INDENT, "yes");
	// trans.transform(domSource, result);
	// return true;
	// } catch (ParserConfigurationException e) {
	// e.printStackTrace();
	// } catch (TransformerConfigurationException e) {
	// e.printStackTrace();
	// } catch (TransformerException e) {
	// e.printStackTrace();
	// }
	// return false;
	// }

	public static void loadLaisimTheme(Context context, String input,
			boolean fromAsset) {
		DocumentBuilderFactory docFactory = DocumentBuilderFactory
				.newInstance();
		Document doc;
		if (mSelectedTheme == null)
			mSelectedTheme = new Theme();
		try {

			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			if (fromAsset) {
				InputStream is = context.getAssets().open(input);
				doc = docBuilder.parse(is);
				is.close();
			} else {
				File xmlFile = new File(input);
				doc = docBuilder.parse(xmlFile);
			}

			doc.getDocumentElement().normalize();

			// get all node with the tag name is LaisimTheme
			NodeList nodeStuList = doc.getElementsByTagName("LaisimTheme");

			// we only have 1 node, so the first node is wanted node
			Node node = nodeStuList.item(0);

			if (node.getNodeType() == Node.ELEMENT_NODE) {
				Element e = (Element) node;
				String text = e.getElementsByTagName("AuthorName").item(0)
						.getTextContent();
				mSelectedTheme.setAuthorName(text);

				text = e.getElementsByTagName("AuthorInfo").item(0)
						.getTextContent();
				mSelectedTheme.setAuthorInfo(text);

				text = e.getElementsByTagName("ThemeName").item(0)
						.getTextContent();
				mSelectedTheme.setThemeName(text);

				text = e.getElementsByTagName("MainBackgroundColor").item(0)
						.getTextContent();
				mSelectedTheme.setMainBackgroundColor(text);

				text = e.getElementsByTagName("HSTitleColor").item(0)
						.getTextContent();
				mSelectedTheme.setHSTitleColor(text);

				text = e.getElementsByTagName("HSArtistColor").item(0)
						.getTextContent();

				mSelectedTheme.setHSArtistColor(text);

				text = e.getElementsByTagName("HSBackgroundAlpha").item(0)
						.getTextContent();
				float valueF = Float.parseFloat(text);
				mSelectedTheme.setHSBackgroundAlpha(valueF);

				text = e.getElementsByTagName("FSBackgroundAlpha").item(0)
						.getTextContent();
				valueF = Float.parseFloat(text);
				mSelectedTheme.setFSBackgroundAlpha(valueF);

				text = e.getElementsByTagName("MPAAlhpha").item(0)
						.getTextContent();
				valueF = Float.parseFloat(text);
				mSelectedTheme.setMPAAlhpha(valueF);

				text = e.getElementsByTagName("MPLSColor").item(0)
						.getTextContent();

				mSelectedTheme.setMPLSColor(text);

				text = e.getElementsByTagName("MPLSSize").item(0)
						.getTextContent();
				valueF = Float.parseFloat(text);
				mSelectedTheme.setMPLSSize(valueF);

				text = e.getElementsByTagName("MPLSAlpha").item(0)
						.getTextContent();
				valueF = Float.parseFloat(text);
				mSelectedTheme.setMPLSAlpha(valueF);

				text = e.getElementsByTagName("MPLBackgroundAlpha").item(0)
						.getTextContent();
				valueF = Float.parseFloat(text);
				mSelectedTheme.setMPLBackgroundAlpha(valueF);

				text = e.getElementsByTagName("MPLNormalArtistColor").item(0)
						.getTextContent();
				mSelectedTheme.setMPLNormalArtistColor(text);

				text = e.getElementsByTagName("MPLNormalArtistSize").item(0)
						.getTextContent();
				valueF = Float.parseFloat(text);
				mSelectedTheme.setMPLNormalArtistSize(valueF);

				text = e.getElementsByTagName("MPLNormalTitleColor").item(0)
						.getTextContent();

				mSelectedTheme.setMPLNormalTitleColor(text);

				text = e.getElementsByTagName("MPLNormalTitleSize").item(0)
						.getTextContent();
				valueF = Float.parseFloat(text);
				mSelectedTheme.setMPLNormalTitleSize(valueF);

				text = e.getElementsByTagName("MPLSelectedArtistColor").item(0)
						.getTextContent();

				mSelectedTheme.setMPLSelectedArtistColor(text);

				text = e.getElementsByTagName("MPLSelectedArtistSize").item(0)
						.getTextContent();
				valueF = Float.parseFloat(text);
				mSelectedTheme.setMPLSelectedArtistSize(valueF);

				text = e.getElementsByTagName("MPLSelectedTitleColor").item(0)
						.getTextContent();

				mSelectedTheme.setMPLSelectedTitleColor(text);

				text = e.getElementsByTagName("MPLSelectedTitleSize").item(0)
						.getTextContent();
				valueF = Float.parseFloat(text);
				mSelectedTheme.setMPLSelectedTitleSize(valueF);

				text = e.getElementsByTagName("NormalTextColor").item(0)
						.getTextContent();

				mSelectedTheme.setNormalTextColor(text);

				text = e.getElementsByTagName("SelectedTextColor").item(0)
						.getTextContent();

				mSelectedTheme.setSelectedTextColor(text);

				text = e.getElementsByTagName("NormalItemListAlpha").item(0)
						.getTextContent();
				valueF = Float.parseFloat(text);
				mSelectedTheme.setNormalItemListAlpha(valueF);

				text = e.getElementsByTagName("SelectedItemListAlpha").item(0)
						.getTextContent();
				valueF = Float.parseFloat(text);
				mSelectedTheme.setSelectedItemListAlpha(valueF);

				text = e.getElementsByTagName("DialogBackgroundColor").item(0)
						.getTextContent();

				mSelectedTheme.setDialogBackgroundColor(text);

				text = e.getElementsByTagName("DialogTextColor").item(0)
						.getTextContent();

				mSelectedTheme.setDialogTextColor(text);

				text = e.getElementsByTagName("DialogTextSelectedColor")
						.item(0).getTextContent();

				mSelectedTheme.setDialogTextSelectedColor(text);

				text = e.getElementsByTagName("TransitionAlpha").item(0)
						.getTextContent();
				valueF = Float.parseFloat(text);
				mSelectedTheme.setTransitionAlpha(valueF);

				text = e.getElementsByTagName("GrowColorEffect").item(0)
						.getTextContent();
				mSelectedTheme.setGrowColorEffect(text);

				text = e.getElementsByTagName("EdgeColorEffect").item(0)
						.getTextContent();
				mSelectedTheme.setEdgeColorEffect(text);

				text = e.getElementsByTagName("ToastColor").item(0)
						.getTextContent();
				mSelectedTheme.setToastMessageColor(text);

				text = e.getElementsByTagName("ToastSize").item(0)
						.getTextContent();
				valueF = Float.parseFloat(text);
				mSelectedTheme.setToastMessageSize(valueF);

			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NullPointerException ne) {
			// this theme must have an error
			// restore to default
			DataBaseAccess db = DataBaseAccess.getInstance(context
					.getApplicationContext());
			db.setSelectedTheme(0);
		}

	}

	public static boolean loadSkin(Context c, int type) {
		DataBaseAccess db = DataBaseAccess.getInstance(c
				.getApplicationContext());
		SkinItem tI = db.getSelectedSkin();
		if (tI == null) {
			importBuiltInSkin(db);
			tI = db.getSelectedSkin();
		}
		int id = tI.id;
		String url = "default.skin";
		if (id <= 1) {
			switch (id) {
			case 1: // default
				url = "default.skin";
				break;
			}
			ThemeManager.loadLaisimSkin(c, url, true, type);
		} else {
			// Load skin from file
			url = tI.address;
			ThemeManager.loadLaisimSkin(c, url, false, type);
		}

		return true;

	}

	private static boolean loadLaisimSkin(Context context, String input,
			boolean fromAsset, int type) {
		byte[] buffer = new byte[4069];
		ZipInputStream zis;
		if (mSelectedSkin == null)
			mSelectedSkin = new Skin();
		try {
			if (fromAsset) {
				zis = new ZipInputStream(context.getAssets().open(input));
			} else {
				zis = new ZipInputStream(new FileInputStream(input));
			}
			ZipEntry ze = zis.getNextEntry();
			while (ze != null) {
				String fileName = ze.getName();
				if (type == PLAYER_SERVICE) {
					if (fileName.equals("img_default_album_art.png")) {
						ByteArrayOutputStream out = new ByteArrayOutputStream();
						int len;
						while ((len = zis.read(buffer)) > 0) {
							out.write(buffer, 0, len);
						}
						out.flush();
						loadSkinByActivity(context, type, fileName,
								out.toByteArray());

					}
					if (fileName.equals("skinExtraInfo.si")) {
						ByteArrayOutputStream out = new ByteArrayOutputStream();
						int len;
						while ((len = zis.read(buffer)) > 0) {
							out.write(buffer, 0, len);
						}
						out.flush();
						loadSkinByActivity(context, type, fileName,
								out.toByteArray());
						// if (mSelectedSkin.getAuthorInfo() == null) {
						InputStream is = new ByteArrayInputStream(
								out.toByteArray());
						loadSkinInfo(context, is);
						// }
						out.close();
						zis.closeEntry();
						zis.close();
						return true;
					}
					ze = zis.getNextEntry();
					continue;
				}
				if (fileName.endsWith(".lst")) {
					// get path to external storage (SD card)
					String path = Environment.getExternalStorageDirectory()
							+ "/Laisim/Theme/";
					File f = new File(path);

					if (!f.isDirectory()) {
						// create storage directories, if they don't exist
						f.mkdirs();
					}
					path += fileName;
					f = new File(path);
					if (!f.exists()) {
						FileOutputStream fos = new FileOutputStream(f);
						int len1;
						while ((len1 = zis.read(buffer)) > 0) {
							fos.write(buffer, 0, len1);
						}
						fos.flush();
						fos.close();
					}
					ze = zis.getNextEntry();
					continue;
				}
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				int len;
				while ((len = zis.read(buffer)) > 0) {
					out.write(buffer, 0, len);
				}
				out.flush();
				if (fileName.equals("skinExtraInfo.si")) {
					// if (mSelectedSkin.getSkinName() == null) {
					InputStream is = new ByteArrayInputStream(out.toByteArray());
					loadSkinInfo(context, is);
					// }
				} else {
					loadSkinByActivity(context, type, fileName,
							out.toByteArray());

				}
				out.close();
				ze = zis.getNextEntry();
			}
			zis.closeEntry();
			zis.close();
			return true;

		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	private static void loadSkinInfo(Context c, InputStream input) {
		Document doc;
		try {
			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			doc = docBuilder.parse(input);

			doc.getDocumentElement().normalize();

			// get all node with the tag name is LaisimTheme
			NodeList nodeStuList = doc.getElementsByTagName("LaisimSkin");

			// we only have 1 node, so the first node is wanted node
			Node node = nodeStuList.item(0);

			if (node.getNodeType() == Node.ELEMENT_NODE) {
				Element e = (Element) node;

				String text = e.getElementsByTagName("AuthorName").item(0)
						.getTextContent();
				mSelectedSkin.setAuthorName(text);

				text = e.getElementsByTagName("AuthorInfo").item(0)
						.getTextContent();
				mSelectedSkin.setAuthorInfo(text);

				text = e.getElementsByTagName("SkinName").item(0)
						.getTextContent();
				mSelectedSkin.setSkinName(text);

				// horizontal progress (Slim progress)
				text = e.getElementsByTagName(
						"HorizontalProgressBarBackgroundColor").item(0)
						.getTextContent();
				mSelectedSkin.setProgressBarHorizontalBackgroundColor(text);

				text = e.getElementsByTagName(
						"HorizontalProgressBarProgressColor").item(0)
						.getTextContent();
				mSelectedSkin.setProgressBarHorizontalProgressColor(text);

				text = e.getElementsByTagName(
						"HorizontalProgressBarBackgroundWidth").item(0)
						.getTextContent();
				float valF = Float.parseFloat(text);
				mSelectedSkin.setProgressBarHorizontalBackgroundWidth(valF);

				text = e.getElementsByTagName(
						"HorizontalProgressBarProgressWidth").item(0)
						.getTextContent();
				valF = Float.parseFloat(text);
				mSelectedSkin.setProgressBarHorizontalProgressWidth(valF);

				// vertical progress (Slim vertical progress)
				text = e.getElementsByTagName(
						"VerticalProgressBarBackgroundColor").item(0)
						.getTextContent();
				mSelectedSkin.setProgressVerticalBackgroundColor(text);

				text = e.getElementsByTagName(
						"VerticalProgressBarInnerColorPressed").item(0)
						.getTextContent();
				mSelectedSkin.setProgressVerticalInnerColorPressed(text);

				text = e.getElementsByTagName(
						"VerticalProgressBarProgressWidth").item(0)
						.getTextContent();
				valF = Float.parseFloat(text);
				mSelectedSkin.setProgressVerticalBackgroundWidth(valF);
				input.close();
			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NullPointerException ne) {
			DataBaseAccess db = DataBaseAccess.getInstance(c
					.getApplicationContext());
			db.setSelectedSkin(0);
		}

	}

	// private static void buildSkin(Context c, String fileName, byte[] data) {
	// BitmapDrawable img = new BitmapDrawable(c.getResources(),
	// BitmapFactory.decodeByteArray(data, 0, data.length));
	// if (fileName.equals("img_btn_bassboost_off.png")) {
	// mSelectedSkin.setBassBtnOff(img);
	// } else if (fileName.equals("img_btn_bassboost_on.png")) {
	// mSelectedSkin.setBassBtnOn(img);
	// } else if (fileName.equals("img_btn_eq_off.png")) {
	// mSelectedSkin.setEqBtnOff(img);
	// } else if (fileName.equals("img_btn_eq_on.png")) {
	// mSelectedSkin.setEqBtnOn(img);
	// } else if (fileName.equals("img_btn_favorite_off.png")) {
	// mSelectedSkin.setFavoriteBtnOff(img);
	// } else if (fileName.equals("img_btn_favorite_on.png")) {
	// mSelectedSkin.setFavoriteBtnOn(img);
	// } else if (fileName.equals("img_btn_shuffle_off.png")) {
	// mSelectedSkin.setShuffleBtnOff(img);
	// } else if (fileName.equals("img_btn_shuffle_on.png")) {
	// mSelectedSkin.setShuffleBtnOn(img);
	// } else if (fileName.equals("img_btn_repeat_all.png")) {
	// mSelectedSkin.setRepeatBtnAll(img);
	// } else if (fileName.equals("img_btn_repeat_off.png")) {
	// mSelectedSkin.setRepeatBtnOff(img);
	// } else if (fileName.equals("img_btn_repeat_one.png")) {
	// mSelectedSkin.setRepeatBtnOne(img);
	// } else if (fileName.equals("img_btn_next.png")) {
	// mSelectedSkin.setNextBtnNormal(img);
	// } else if (fileName.equals("img_btn_next_pressed.png")) {
	// mSelectedSkin.setNextBtnPressed(img);
	// } else if (fileName.equals("img_btn_previous.png")) {
	// mSelectedSkin.setPrevBtnNormal(img);
	// } else if (fileName.equals("img_btn_previous_pressed.png")) {
	// mSelectedSkin.setPrevBtnPressed(img);
	// } else if (fileName.equals("img_btn_pause.png")) {
	// mSelectedSkin.setPauseBtnNormal(img);
	// } else if (fileName.equals("img_btn_pause_pressed.png")) {
	// mSelectedSkin.setPauseBtnPressed(img);
	// } else if (fileName.equals("img_btn_play.png")) {
	// mSelectedSkin.setPlayBtnNormal(img);
	// } else if (fileName.equals("img_btn_play_pressed.png")) {
	// mSelectedSkin.setPlayBtnPressed(img);
	// }
	// // lib screen
	// else if (fileName.equals("img_album.png")) {
	// mSelectedSkin.setLibScreenAlbum(img);
	// } else if (fileName.equals("img_all_song.png")) {
	// mSelectedSkin.setLibScreenAllSong(img);
	// } else if (fileName.equals("img_artist.png")) {
	// mSelectedSkin.setLibScreenArtist(img);
	// } else if (fileName.equals("img_favorite_song.png")) {
	// mSelectedSkin.setLibScreenFavorite(img);
	// } else if (fileName.equals("img_folder.png")) {
	// mSelectedSkin.setLibScreenFolder(img);
	// } else if (fileName.equals("img_genre.png")) {
	// mSelectedSkin.setLibScreenGenre(img);
	// } else if (fileName.equals("img_lyrics.png")) {
	// mSelectedSkin.setLibScreenHasLyrics(img);
	// } else if (fileName.equals("img_most_play.png")) {
	// mSelectedSkin.setLibScreenMostPlayed(img);
	// } else if (fileName.equals("img_play_list.png")) {
	// mSelectedSkin.setLibScreenPlayList(img);
	// } else if (fileName.equals("img_default_album_art.png")) {
	// mSelectedSkin.setDefaultAlbum(img);
	// }
	// effect screen
	// else if (fileName.equals("img_progress_thumb.png")) {
	// mSelectedSkin.setProgressThumbNormal(img);
	// } else if (fileName.equals("img_progress_thumb_pressed.png")) {
	// mSelectedSkin.setProgressThumbPressed(img);
	// } else if (fileName.equals("img_progress_circular_bg.png")) {
	// mSelectedSkin.setProgressCircularBackgroundNormal(img);
	// } else if (fileName.equals("img_progress_circular_bg_pressed.png")) {
	// mSelectedSkin.setProgressCircularBackgroundPressed(img);
	// } else if (fileName.equals("img_progress_circular_thumb.png")) {
	// mSelectedSkin.setProgressCircularThumb(img);
	// } else if (fileName.equals("img_effect_act_above_background.png")) {
	// mSelectedSkin.setEffectActivityAboveBackground(img);
	// } else if (fileName.equals("img_effect_act_below_background.png")) {
	// mSelectedSkin.setEffectActivityBelowBackground(img);
	// } else if (fileName.equals("img_btn_effect_ams.png")) {
	// mSelectedSkin.setBtnEffectAMS(img);
	// } else if (fileName.equals("img_btn_effect_amg.png")) {
	// mSelectedSkin.setBtnEffectAMG(img);
	// } else if (fileName.equals("img_btn_effect_ama.png")) {
	// mSelectedSkin.setBtnEffectAMA(img);
	// } else if (fileName.equals("img_btn_effect_eq_on_on.png")) {
	// mSelectedSkin.setBtnEffectEqOnOn(img);
	// } else if (fileName.equals("img_btn_effect_eq_on_off.png")) {
	// mSelectedSkin.setBtnEffectEqOnOff(img);
	// } else if (fileName.equals("img_btn_effect_eq_off_on.png")) {
	// mSelectedSkin.setBtnEffectEqOffOn(img);
	// } else if (fileName.equals("img_btn_effect_eq_off_off.png")) {
	// mSelectedSkin.setBtnEffectEqOffOff(img);
	// } else if (fileName.equals("img_btn_effect_bass_on_on.png")) {
	// mSelectedSkin.setBtnEffectBassOnOn(img);
	// } else if (fileName.equals("img_btn_effect_bass_on_off.png")) {
	// mSelectedSkin.setBtnEffectBassOnOff(img);
	// } else if (fileName.equals("img_btn_effect_bass_off_on.png")) {
	// mSelectedSkin.setBtnEffectBassOffOn(img);
	// } else if (fileName.equals("img_btn_effect_bass_off_off.png")) {
	// mSelectedSkin.setBtnEffectBassOffOff(img);
	// } else if (fileName.equals("img_btn_effect_reset.png")) {
	// mSelectedSkin.setBtnEffectReset(img);
	// }

	// }

	public static void loadSkinByActivity(Context c, int type, String fileName,
			byte[] data) {
		BitmapDrawable img = new BitmapDrawable(c.getResources(),
				BitmapFactory.decodeByteArray(data, 0, data.length));
		switch (type) {
		case MAIN_ACTIVYTY:
			if (fileName.equals("img_btn_bassboost_off.png")) {
				mSelectedSkin.setBassBtnOff(img);
			} else if (fileName.equals("img_btn_bassboost_on.png")) {
				mSelectedSkin.setBassBtnOn(img);
			} else if (fileName.equals("img_btn_eq_off.png")) {
				mSelectedSkin.setEqBtnOff(img);
			} else if (fileName.equals("img_btn_eq_on.png")) {
				mSelectedSkin.setEqBtnOn(img);
			} else if (fileName.equals("img_btn_favorite_off.png")) {
				mSelectedSkin.setFavoriteBtnOff(img);
			} else if (fileName.equals("img_btn_favorite_on.png")) {
				mSelectedSkin.setFavoriteBtnOn(img);
			} else if (fileName.equals("img_btn_shuffle_off.png")) {
				mSelectedSkin.setShuffleBtnOff(img);
			} else if (fileName.equals("img_btn_shuffle_on.png")) {
				mSelectedSkin.setShuffleBtnOn(img);
			} else if (fileName.equals("img_btn_repeat_all.png")) {
				mSelectedSkin.setRepeatBtnAll(img);
			} else if (fileName.equals("img_btn_repeat_off.png")) {
				mSelectedSkin.setRepeatBtnOff(img);
			} else if (fileName.equals("img_btn_repeat_one.png")) {
				mSelectedSkin.setRepeatBtnOne(img);
			} else if (fileName.equals("img_btn_next.png")) {
				mSelectedSkin.setNextBtnNormal(img);
			} else if (fileName.equals("img_btn_next_pressed.png")) {
				mSelectedSkin.setNextBtnPressed(img);
			} else if (fileName.equals("img_btn_previous.png")) {
				mSelectedSkin.setPrevBtnNormal(img);
			} else if (fileName.equals("img_btn_previous_pressed.png")) {
				mSelectedSkin.setPrevBtnPressed(img);
			} else if (fileName.equals("img_btn_pause.png")) {
				mSelectedSkin.setPauseBtnNormal(img);
			} else if (fileName.equals("img_btn_pause_pressed.png")) {
				mSelectedSkin.setPauseBtnPressed(img);
			} else if (fileName.equals("img_btn_play.png")) {
				mSelectedSkin.setPlayBtnNormal(img);
			} else if (fileName.equals("img_btn_play_pressed.png")) {
				mSelectedSkin.setPlayBtnPressed(img);
			} else if (fileName.equals("img_default_album_art.png")) {
				mSelectedSkin.setDefaultAlbum(img);
				// copy default album art to storage
				FileOutputStream f;
				try {
					f = c.openFileOutput("daa", Context.MODE_PRIVATE);
					f.write(data, 0, data.length);
					f.flush();
					f.close();
				} catch (FileNotFoundException e) {
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			break;
		case SOUND_EFFECT_ACTIVYTY:
			if (fileName.equals("img_progress_thumb.png")) {
				mSelectedSkin.setProgressVerticalThumbNormal(img);
			} else if (fileName.equals("img_progress_thumb_pressed.png")) {
				mSelectedSkin.setProgressVerticalThumbPressed(img);
			} else if (fileName.equals("img_progress_circular_bg.png")) {
				mSelectedSkin.setProgressCircularBackgroundNormal(img);
			} else if (fileName.equals("img_progress_circular_bg_pressed.png")) {
				mSelectedSkin.setProgressCircularBackgroundPressed(img);
			} else if (fileName.equals("img_progress_circular_thumb.png")) {
				mSelectedSkin.setProgressCircularThumb(img);
			} else if (fileName.equals("img_effect_act_above_background.png")) {
				mSelectedSkin.setEffectActivityAboveBackground(img);
			} else if (fileName.equals("img_effect_act_below_background.png")) {
				mSelectedSkin.setEffectActivityBelowBackground(img);
			} else if (fileName.equals("img_btn_effect_ams.png")) {
				mSelectedSkin.setBtnEffectAMS(img);
			} else if (fileName.equals("img_btn_effect_amg.png")) {
				mSelectedSkin.setBtnEffectAMG(img);
			} else if (fileName.equals("img_btn_effect_ama.png")) {
				mSelectedSkin.setBtnEffectAMA(img);
			} else if (fileName.equals("img_btn_effect_eq_on_on.png")) {
				mSelectedSkin.setBtnEffectEqOnOn(img);
			} else if (fileName.equals("img_btn_effect_eq_on_off.png")) {
				mSelectedSkin.setBtnEffectEqOnOff(img);
			} else if (fileName.equals("img_btn_effect_eq_off_on.png")) {
				mSelectedSkin.setBtnEffectEqOffOn(img);
			} else if (fileName.equals("img_btn_effect_eq_off_off.png")) {
				mSelectedSkin.setBtnEffectEqOffOff(img);
			} else if (fileName.equals("img_btn_effect_bass_on_on.png")) {
				mSelectedSkin.setBtnEffectBassOnOn(img);
			} else if (fileName.equals("img_btn_effect_bass_on_off.png")) {
				mSelectedSkin.setBtnEffectBassOnOff(img);
			} else if (fileName.equals("img_btn_effect_bass_off_on.png")) {
				mSelectedSkin.setBtnEffectBassOffOn(img);
			} else if (fileName.equals("img_btn_effect_bass_off_off.png")) {
				mSelectedSkin.setBtnEffectBassOffOff(img);
			} else if (fileName.equals("img_btn_effect_reset.png")) {
				mSelectedSkin.setBtnEffectReset(img);
			}
			break;
		case LIBRARY_ACTIVYTY:
			if (fileName.equals("img_album.png")) {
				mSelectedSkin.setLibScreenAlbum(img);
			} else if (fileName.equals("img_all_song.png")) {
				mSelectedSkin.setLibScreenAllSong(img);
			} else if (fileName.equals("img_artist.png")) {
				mSelectedSkin.setLibScreenArtist(img);
			} else if (fileName.equals("img_favorite_song.png")) {
				mSelectedSkin.setLibScreenFavorite(img);
			} else if (fileName.equals("img_folder.png")) {
				mSelectedSkin.setLibScreenFolder(img);
			} else if (fileName.equals("img_genre.png")) {
				mSelectedSkin.setLibScreenGenre(img);
			} else if (fileName.equals("img_lyrics.png")) {
				mSelectedSkin.setLibScreenHasLyrics(img);
			} else if (fileName.equals("img_most_play.png")) {
				mSelectedSkin.setLibScreenMostPlayed(img);
			} else if (fileName.equals("img_play_list.png")) {
				mSelectedSkin.setLibScreenPlayList(img);
			}
			break;
		case PLAYER_SERVICE:
			if (fileName.equals("img_default_album_art.png")) {
				mSelectedSkin.setDefaultAlbum(img);
			}
			break;
		}
		data = null;
	}

	/**
	 * Release all resource hold by skin object
	 */
	public static void releaseAll() {
		if (mSelectedSkin == null)
			return;
		mSelectedSkin.releaseAll();
		mSelectedSkin = null;
	}

	public static void release(int type) {
		if (mSelectedSkin == null)
			return;
		mSelectedSkin.release(type);
	}
}
